package com.laixu.estore.web.controller;

import com.laixu.estore.bean.vo.ShopcarVo;
import com.laixu.estore.service.ShopCarService;
import com.laixu.estore.web.response.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: vanse(lc)
 * @Date: 2025/3/10-03-10-17:22
 * @Description：com.briup.estore.web.controller GET 地址栏
 * POST 请求体
 */
@RestController // 前后端分离 json 不返回页面
@RequestMapping("/shopCar")
@RequiredArgsConstructor // lombok
@Slf4j// SLf4j lombok提供的日志对象
public class ShopcarController {
    private final ShopCarService shopCarService;

    // 添加新记录 新的书籍 post
    // 维护旧记录 老的书籍 put
    // 查询记录 get
    // 删除记录 delete
    @PutMapping("/{bookId}")
    public Result addCar(@PathVariable Integer bookId,
                         HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        shopCarService.addCar(Integer.parseInt(userId), bookId);
        return Result.success();
    }

    @GetMapping("/")
    public Result getShopCar(
            HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        List<ShopcarVo> shopcars = shopCarService.getShopCar(userId);
        return Result.success(shopcars);
    }

}